#!usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author: muchen
@file: file_utils.py
@time: 2024/12/24
@desc: 文件工具类
"""
import os
import yaml


# 获取文件路径
def get_file_path(filename):
    return os.path.join(os.path.dirname(os.path.dirname(__file__)), 'data', filename)


# 读取yaml文件内容
def open_yaml(filename):
    with open(get_file_path(filename), 'r', encoding='utf-8') as f:
        return yaml.safe_load(f)  # safe_load将yaml文件转换为字典


# 写yaml文件
def write_yaml(filename, data):
    with open(get_file_path(filename), 'w', encoding='utf-8') as f:
        yaml.safe_dump(data, f, allow_unicode=True)


# 获取日志文件路径
def get_log_path():
    return os.path.join(os.path.dirname(os.path.dirname(__file__)), 'log')


if __name__ == '__main__':
    # res = get_file_path("test_data.yaml")
    # if os.path.exists(res):
    #     print(res)
    #     print("student: ", open_yaml("test_data.yaml").get("student"))
    #     print("skills: ", open_yaml("test_data.yaml").get("skills"))
    #     print("students: ", open_yaml("test_data.yaml").get("students"))
    #     print("test_data_login: ", open_yaml("test_data.yaml").get("test_data_login"))

    write_yaml("token.yaml", {"test_data_login": [{"username": "admin", "passwd": "admin", "expectVal": "登录成功"}]})
    print("写入成功")
